﻿Imports OutlookStyleControls
Imports System.Collections
Imports System.ComponentModel

Public Class frmLoHang
    'dung de luu gia tri loc cua lo hang
    'sau do truyen vao form Loc 
    Public fCallPhieuNX As Form = Nothing
    Dim obj_MatHang As CMatHang
    Private ma_loaikho As String
    Private ma_kho As String
    Private dpnx_id As Integer
    Private ngay As Date
    Private slndkkh As Double = Nothing
    Private slxdkkh As Double = Nothing
    Private sltonkh As Double = Nothing


    'Code: Hoang Tuan
    'Bien nay dung de dem so lan nguoi dung double click de add mat hang moi vao phieu nhap
    'Neu count = 1 thi add mot mat hang duy nhat
    'neu count > 1 thi add nhieu mat hang mot luc
    Private count As Integer = 0

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'KhoiTaoDMKho()
    End Sub

    Public Sub New(ByVal loaikho As String, ByVal kho As String)
        'dung khoi tao dm lo hang cho phieu xuat
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ma_loaikho = loaikho
        ma_kho = kho
        dpnx_id = Nothing
        ngay = Now.Date
        Me.Text = "Danh mục mặt hàng ở " + CDoiTuongs.GetNameByMa(enumDOITUONG.KHO, kho) + " có số lượng tồn tính đến ngày " + Now.Date.ToString(FormatDateStyle())
    End Sub

    Public Sub New(ByVal dpnxid As Integer, ByVal ngaynx As Date, ByVal loaikho As String, ByVal kho As String)
        'dung khoi tao dm lo hang cho phieu xuat
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ma_loaikho = loaikho
        ma_kho = kho
        dpnx_id = dpnxid
        ngay = ngaynx
        Me.Text = "Danh mục mặt hàng ở " + CDoiTuongs.GetNameByMa(enumDOITUONG.KHO, kho) + " có số lượng tồn tính đến ngày " + ngaynx.ToString(FormatDateStyle())
    End Sub

    'Private Sub dtgThuoc_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgThuoc.CellDoubleClick
    '    Try
    '        If fCallPhieuNX IsNot Nothing Then
    '            count += 1
    '            Dim frm As frmPhieuNX = CType(fCallPhieuNX, frmPhieuNX)
    '            frm.AddNhomHang(dtgThuoc.Rows(e.RowIndex).Cells("mathuoc").Value, count)
    '        End If
    '    Catch ex As Exception
    '        ShowMessages("Error: frmVattu dtgThuoc_CellDoubleClick: " + ex.ToString())
    '    End Try
    'End Sub

    Private Sub lvDMMathang_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvDMMatHang.DoubleClick
        lblSLKH.Visible = True
        txtSLKH.Text = ""
        txtSLKH.Visible = True
        txtSLKH.Focus()
    End Sub

    Private Sub lvDMMathang_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvDMMatHang.KeyDown
        If e.KeyCode = Keys.Enter Then
            lvDMMathang_DoubleClick(Nothing, Nothing)
        ElseIf e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub txtTen_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTen.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub txtTen_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTen.TextChanged
        Dim lvItem As ListViewItem
        lvItem = lvDMMatHang.FindItemWithText(txtTen.Text)
        If lvItem IsNot Nothing Then
            lvDMMatHang.Focus()
            lvItem.Selected = True
            lvItem.Focused = True
            lvItem.EnsureVisible()
            txtTen.Focus()
        End If
    End Sub

    Private Sub frmLoHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Create columns for the items and subitems.
        lvDMMatHang.Columns.Add("Tên mặt hàng", 178, HorizontalAlignment.Left)
        lvDMMatHang.Columns.Add("Lô hàng", 117, HorizontalAlignment.Left)
        lvDMMatHang.Columns.Add("Hạn SD", 56, HorizontalAlignment.Left)
        lvDMMatHang.Columns.Add("SL Tồn", 59, HorizontalAlignment.Right)
        lvDMMatHang.Columns.Add("Mã mặt hàng", 0, HorizontalAlignment.Center)
        lblSLKH.Visible = False
        txtSLKH.Visible = False
        Init()
    End Sub

    Public Sub Init()
        Dim strWhere As String
        strWhere = "where lk = '" + ma_loaikho + "' and kho = '" + ma_kho + "' and sltonkh > 0 and ngtton <= '" + Format(ngay, "M/d/yyyy") + "'"
        Dim aryDTs As ArrayList = CMatHangs.GetMatHangList(strWhere)
        lvDMMatHang.Items.Clear()
        Dim item As ListViewItem
        For Each objMH As CMatHang In aryDTs
            item = New ListViewItem(objMH.Ten)
            item.SubItems.Add(objMH.Lo)
            item.SubItems.Add(objMH.HanSD)
            CMatHangs.GetSLTonDenNgay(dpnx_id, objMH.MatHang_ID, ngay, sltonkh, Nothing, Nothing, Nothing)
            item.SubItems.Add(sltonkh.ToString(FormatMoneyStyle()))
            item.SubItems.Add(objMH.MatHang_ID)
            lvDMMatHang.Items.Add(item)
        Next

        If lvDMMatHang.Items.Count > 0 Then
            lvDMMatHang.Focus()
            lvDMMatHang.Items(0).Selected = True
        Else
            lblMaCode.Text = ""
            lblTenHang.Text = ""
            lblLoHang.Text = ""
            lblCCL.Text = ""
            lblHSD.Text = ""
            lblDonGia.Text = ""
            lblDVT.Text = ""
            lblTonKH.Text = ""
        End If
    End Sub

    Public Sub AddMathang()
        If fCallPhieuNX IsNot Nothing Then
            If Not IsNothing(Me.lvDMMatHang.SelectedItems(0)) Then
                count += 1
                If fCallPhieuNX.Name Is frmPhieuNX.Name Then
                    Dim frm As frmPhieuNX = CType(fCallPhieuNX, frmPhieuNX)
                    frm.AddMatHang(obj_MatHang, Double.Parse(txtSLKH.Text), count)
                ElseIf fCallPhieuNX.Name Is frmPhieuNXNoiBo.Name Then
                    Dim frm As frmPhieuNXNoiBo = CType(fCallPhieuNX, frmPhieuNXNoiBo)
                    frm.AddMatHang(obj_MatHang, Double.Parse(txtSLKH.Text), count)
                ElseIf fCallPhieuNX.Name Is frmPhieuNXDCSS.Name Then
                    Dim frm As frmPhieuNXDCSS = CType(fCallPhieuNX, frmPhieuNXDCSS)
                    frm.AddMatHang(obj_MatHang, Double.Parse(txtSLKH.Text), count)
                End If
            End If
        End If
    End Sub

    Private Sub lvDMMatHang_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvDMMatHang.SelectedIndexChanged
        Try
            If lvDMMatHang.Items.Count > 0 Then
                obj_MatHang = CMatHangs.GetMatHangByMa(lvDMMatHang.SelectedItems(0).SubItems(4).Text)
                lblTenHang.Text = obj_MatHang.Ten
                lblMaCode.Text = obj_MatHang.Ma + "-" + obj_MatHang.Code
                lblLoHang.Text = obj_MatHang.Lo
                lblCCL.Text = obj_MatHang.H
                lblHSD.Text = obj_MatHang.HanSD
                lblNgTTon.Text = obj_MatHang.NgTTon.ToString(FormatDateStyle())

                If obj_MatHang.Gle > 0 Then
                    lblDonGia.Text = obj_MatHang.Gle.ToString(FormatMoneyStyle())
                Else
                    lblDonGia.Text = ""
                End If

                lblDVT.Text = obj_MatHang.DVT
                lblLoaiKho.Text = CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.LOAIKHO, obj_MatHang.LK).Ten
                If obj_MatHang.SLKK11 > 0 Then
                    lblSLKK.Text = obj_MatHang.SLKK11.ToString(FormatQuantityStyle())
                Else
                    lblSLKK.Text = "0"
                End If

                If obj_MatHang.SLSS11 > 0 Then
                    lblSLSSDauNam.Text = obj_MatHang.SLSS11.ToString(FormatQuantityStyle())
                Else
                    lblSLSSDauNam.Text = "0"
                End If
                'Tinh slton den tu truoc den ngay = "ngay" la bao nhieu
                CMatHangs.GetSLTonDenNgay(dpnx_id, obj_MatHang.MatHang_ID, ngay, sltonkh, Nothing, slndkkh, slxdkkh)
                If sltonkh > 0 Then
                    lblTonKH.Text = sltonkh.ToString(FormatQuantityStyle()) 'obj_MatHang.SLTonKH.ToString(FormatQuantityStyle())
                Else
                    lblTonKH.Text = "0"
                End If

                If slndkkh > 0 Then
                    lblLuyTichNhapKH.Text = slndkkh.ToString(FormatQuantityStyle()) 'obj_MatHang.SLNDKKH.ToString(FormatQuantityStyle())
                Else
                    lblLuyTichNhapKH.Text = "0"
                End If

                If slxdkkh > 0 Then
                    lblLuyTichXuatKH.Text = slxdkkh.ToString(FormatQuantityStyle()) 'obj_MatHang.SLXDKKH.ToString(FormatQuantityStyle())
                Else
                    lblLuyTichXuatKH.Text = "0"
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Public countEnter As Integer = 0
    Private Sub txtSLKH_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSLKH.KeyDown
        If e.KeyCode = Keys.Enter Then
            If countEnter = 0 Then
                Dim value As Double
                countEnter += 1
                If txtSLKH.Text = "" Then
                    ShowMessages("Số lượng xuất chưa nhập.")
                    Exit Sub
                End If

                Try
                    value = Double.Parse(txtSLKH.Text)
                Catch ex As Exception
                    ShowMessages("Số lượng xuất không hợp lệ.")
                    Exit Sub
                End Try

                If value > Double.Parse(lblTonKH.Text) Then
                    ShowMessages("Số lượng xuất phải nhỏ hơn số lượng tồn.")
                    Exit Sub
                End If
                txtSLKH.Text = value.ToString(FormatQuantityStyle())
                txtSLKH.SelectionStart = txtSLKH.TextLength
            ElseIf countEnter = 1 Then
                AddMathang()
                lblSLKH.Visible = False
                txtSLKH.Visible = False
                lvDMMatHang.Focus()
            End If
        ElseIf e.KeyCode = Keys.Escape Then
            Me.Close()
        Else
            countEnter = 0
        End If
    End Sub
End Class